<?php

namespace app\common\mongodb;

use lib\Singleton;
use think\Model;

class Mon extends Model
{
    use Singleton;

    /**
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * @var return value
     */
    const  RETURN_VALUE = 0;

    /**
     * @var string
     */
    public $connection;

    /**
     * @param array $data
     */
    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->connection = 'mongodb';
    }

    /**
     * 插入数据
     * @param array $data
     * @return int
     * @Function:insertTable
     * @Date: 2022/11/12
     * @Time: 16:01
     */
    public static function insertTable(array $data = []): int
    {
        if (!is_array($data) || count($data) < 1) {
            return self::RETURN_VALUE;
        }
        return self::insert($data);
    }

    /**
     * 插入数据(全部)
     * @param array $data
     * @return int
     * @Function:insertTableALL
     * @Date: 2022/11/12
     * @Time: 16:01
     */
    public static function insertTableALL(array $data = []): int
    {
        if (!is_array($data) || count($data) < 1) {
            return self::RETURN_VALUE;
        }
        return self::insertAll($data);
    }

    /**
     * 更新数据
     * @param $where
     * @param $data
     * @return Mon|int
     * @Function:updateTable
     * @Date: 2022/11/12
     * @Time: 16:01
     */
    public function updateTable($where = [], $data = [])
    {
        if (!is_array($data) || count($data) < 1 || count($where) < 1) {
            return self::RETURN_VALUE;
        }
        return $this->where($where)->update($data);
    }

    /**
     * 静态更新数据
     * @param $where
     * @param $data
     * @return Mon|int
     * @Function:updateTableInfoData
     * @Date: 2022/11/12
     * @Time: 16:01
     */
    public static function updateTableInfoData($where = [], $data = [])
    {
        if (!is_array($data) || count($data) < 1 || count($where) < 1) {
            return self::RETURN_VALUE;
        }
        return self::where($where)->update($data);
    }

    /**
     * 获取数据
     * @param $where
     * @param $field
     * @param $order
     * @return Mon|array|mixed|Model|null
     * @Function:selectData
     * @Date: 2022/11/12
     * @Time: 16:01
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function selectData($where, $field = "*", $order = '_id')
    {
        if (count($where) < 1) {
            return [];
        }
        return self::where($where)->field($field)->order($order)->find();
    }

    /**
     * 查询多条数据
     * @param $where
     * @param $field
     * @return Mon[]|array|\think\Collection
     * @Function:selectDataArray
     * @Date: 2022/11/12
     * @Time: 16:01
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function selectDataArray($where, $field = "*")
    {

        if (count($where) < 1) {
            return [];
        }
        return self::where($where)->field($field)->select();
    }

    /**
     * 查询指定条件下总数量
     * @param $where
     * @return array|int
     * @Function:selectCount
     * @Date: 2022/11/12
     * @Time: 16:01
     * @throws \think\db\exception\DbException
     */
    public static function selectCount($where)
    {
        if (count($where) < 1) {
            return [];
        }
        return self::where($where)->count();
    }

    /**
     * 删除数据
     * @param $where
     * @return bool|int
     * @Function:delData
     * @Date: 2022/11/12
     * @Time: 16:01
     */
    public static function delData($where)
    {
        if (count($where) < 1) {
            return 0;
        }
        return self::where($where)->delete();
    }

    /**
     * 获取消息总数
     * @param $where
     * @return int
     * @Function:getTableCount
     * @Date: 2022/11/12
     * @Time: 16:01
     * @throws \think\db\exception\DbException
     */
    public static function getTableCount($where)
    {
        if (count($where) < 1) {
            return 0;
        }
        return self::where($where)->count();
    }

    /**
     * 分页查询数据
     * @param $where
     * @param $field
     * @param $order
     * @param $page
     * @param $limit
     * @return Mon[]|array|int|\think\Collection
     * @Function:selectTableListByPage
     * @Date: 2022/11/12
     * @Time: 16:00
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function selectTableListByPage($where, $field = "*", $order = "_id", $page = 1, $limit = 10)
    {
        if (count($where) < 1) {
            return 0;
        }
        return self::where($where)->field($field)->order($order, "-1")->page($page, $limit)->select();
    }
}
